<template>
	<view class="dropdown">
		<view class="dropdown-mask" @click="handlerMask" v-show="showMask"></view>
		<view class="selectTitlesBox" @tap="openSelect" :style="{ minWidth: selectWidth + 'rpx', maxWidth: selectWidth2 + 'rpx', fontSize: selectFontSize + 'px', color: fontColor, backgroundColor: bgColor }">
			<view class="modelTitleBox">
				<text>{{ modelTitle }}</text>
				<view :class="selectShow ? 'biaoshi2' : 'biaoshi'"></view>
			</view>
		</view>
		<view class="ul" v-show="selectShow" :style="{ minWidth: selectWidth + 'rpx', maxWidth: selectWidth2 + 'rpx', }">
			<view
				class="li"
				:style="bgIndex === index ? `color: ${fontBgColor}` : `color: ${fontColor}`"
				v-for="(item, index) in list"
				:key="item.value"
				@click="handlerItem(item.value)"
			>
				{{ item.name }}
			</view>
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			selectShow: false,
			showMask: false
		};
	},
	name: 'dropdown',
	props: {
		list: {
			type: Array,
			default: () => []
		},
		bgIndex: {
			type: Number,
			default: 0
		},
		fontColor: {
			type: String,
			default: '#333333'
		},
		fontBgColor: {
			type: String,
			default: '#333333'
		},
		selectWidth: {
			type: Number,
			default: 100
		},
		selectWidth2: {
			type: Number,
			default: 150
		},
		selectFontSize: {
			type: Number,
			default: 14
		},
		bgColor: {
			type: String,
			default: '#ffffff'
		}
	},
	computed: {
		modelTitle() {
			var titles = '';
			this.list.forEach(item => {
				if (item.value === this.bgIndex) {
					titles = item.name;
				}
			});
			return titles;
		}
	},
	methods: {
		openSelect() {
			this.selectShow = !this.selectShow;
			this.showMask = !this.showMask;
		},
		handlerItem(value) {
			this.$emit('select', value);
			this.handlerMask();
		},
		handlerMask() {
			this.selectShow = false;
			this.showMask = false;
		}
	}
};
</script>

<style scoped lang="scss">
.dropdown {
	position: absolute;
	z-index: 100;
}

.ul {
	position: relative;
	z-index: 101;
	list-style: none;
	background-color: #fff;
	touch-action: none;
	// border-radius: 4rpx;
	border-bottom-right-radius: 10px;
	border-bottom-left-radius: 10px;
	padding-left: 0;
	// box-shadow: 10rpx 10rpx 20rpx rgba(224, 32, 32, 0.2);
	.li {
		color: #333;
		padding: 10px 20rpx;
		touch-action: none;
		// border-bottom: 1px solid #e6eaeb;
		font-size: 20rpx;
	}
	.li:last-child {
		border-bottom: none;
	}
}
.dropdown-mask {
	top: 0;
	left: 0;
	position: fixed;
	width: 100vw;
	height: 100vh;
	z-index: 99;
	touch-action: none;
	// background-color: rgba(0, 0, 0, 0.6);
}
.selectTitlesBox {
	padding-top: 3px;
	position: relative;
	z-index: 101;
	.modelTitleBox {
		display: flex;
		align-items: center;
		.biaoshi {
			margin-left: 5px;
			display: inline-block;
			width: 0;
			height: 0;
			border-left: 5px solid transparent;
			border-right: 5px solid transparent;
			border-bottom: 8px solid #9e9fa0;
		}
		.biaoshi2 {
			margin-left: 5px;
			display: inline-block;
			width: 0;
			height: 0;
			border-left: 5px solid transparent;
			border-right: 5px solid transparent;
			border-top: 8px solid #9e9fa0;
		}
	}
}
</style>
